# -*- coding: utf-8 -*-
# @Time    : 2022/3/14 16:52
# @Author  : TangDagui
# @Email   : 3297364807@qq.com
# @File    : Test.py
# @Software: PyCharm
import http.cookiejar
import urllib.parse

url = 'http://bbs.chinaunix.net/member.php?mod=logging&action=login&logsubmit=yes'
headers = {
    f'User-Agent': 'Mozilla/5.0 (Windows NT 10,0: Win64: x64) AppleWebKit/537.36 (KHTMLlike Gecko) Chrome/92.0.4515.159 Safari/537.36'}
data = {f'username'' a877348180''password':'a123456'}
data = urllib.parse.urlencode(data).encode('utf-8')
# 创建一个请求对象,利用Request类来传递urlheheaders数据
request = urllib.request.Request(url, headers=headers, data=data)
# 第一步:使用http.cookiejar.Cookie()创建Cookiejar对象
cookiejar = http.cookiejar.CookieJar()
# 第二步:使用urllib,request中的HTTPCookieProcessor创建cookie处理器,处理传递过来的cookie的信息
processor = urllib.request.HTTPCookieProcessor(cookiejar)
# 第二步:把处理器作为参数构建opener对象
opener = urllib.request.build_opener(processor)
# 安装opener对象,对urllib.request安装
urllib.request.install_opener(opener)
# 利用opener发送请求,得到一个相应结果
response = opener.open(request)
data = response.read()
# print(data)
with open('.webpagellogin.html', 'wb') as writer:
    writer.write(data)
print('保存完毕！ ')



